package com.jzo2o.foundations.handler;


import com.jzo2o.api.foundations.dto.response.RegionSimpleResDTO;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.service.HomeService;
import com.jzo2o.foundations.service.IRegionService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * springCache缓存同步任务
 */

@Slf4j
@Component
public class SpringCacheSyncHandler {

    @Resource
    private IRegionService regionService;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private HomeService homeService;


    /**
     * 已启用区域缓存更新
     * 每日凌晨1点执行
     */
    @XxlJob(value = "activeRegionCacheSync")
    public void activeRegionCacheSync(){

        log.info(">>>>>>>>>>>>>开始进行缓存同步，更新已启用区域");
        //1.清空缓存
        String key = RedisConstants.CacheName.JZ_CACHE+"::ACTIVE_REGION";
        redisTemplate.delete(key);
        //添加新缓存，查询到所有开通的区域
        List<RegionSimpleResDTO> regionSimpleResDTOS = regionService.queryActiveRegionListCache();

        //遍历区域，对每个区域的首页服务列表进行删除缓存在添加缓存
        regionSimpleResDTOS.forEach(item->{
            //key
            String key1 = RedisConstants.CacheName.SERVE_ICON+"::"+item.getId();
            //删除缓存
            redisTemplate.delete(key1);
            //调用首页服务列表的查询方法去添加缓存
            homeService.queryServeIconCategoryByRegionIdCache(item.getId());
        });
        log.info(">>>>>>>>>>>>>更新已启用区域完成");

    }



}
